package com.hanlink.dsp.util;

import android.util.Log;

import java.util.ArrayList;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C) 秦智科技有限责任公司版权所有: 2015/10/11
 *
 * Description:
 *
 * Revision History
 * Date			         Author		  Description
 * 2015/10/11        fonhal       初始创建
 *
 * </pre>
 */
public class LedEffect {

    static int w = 640, h = 960;
    public static int HEIGHT = 65;
    public static int HEIGHT_FREQUENCY = 85;
    private static int music_off_num = 0;
    private static int music_work_num = 0;
    private static boolean cache = false;

    private static int cred = 150;
    private static int cgreen = 50;
    private static int cblue = 0;
    private static int calpha = 0;

    public static int blue = 0;
    public static int green = 50;
    public static int red = 150;

    private static int a = 0;
    public static int changenum = 5;


    public static int[] getColors(byte[] bytes) {
        getcolorchang();
        setcolor(getoldRms(bytes));
        cred = SoundUtil.getColor(cred);
        cgreen = SoundUtil.getColor(cgreen);
        cblue = SoundUtil.getColor(cblue);
        return new int[]{cred, cgreen, cblue};
    }

    private static void setcolor(final Integer i) {
        Log.d("PlayFragment", "i:" + i);
        if (i <= -5000 || i > -3000) {
            int j = (int) (0.050000000000000003D * (double) (Math.abs(i) - 3000));
            cred = (int) (((double) j / 255D) * (double) red);
            cgreen = (int) (((double) j / 255D) * (double) green);
            cblue = (int) (((double) j / 255D) * (double) blue);
        } else if (i <= -3000 || i > -1500) {
            int k = (int) (0.066699999999999995D * (double) (Math.abs(i) - 1500)) + 100;
            cred = (int) (((double) k / 255D) * (double) red);
            cgreen = (int) (((double) k / 255D) * (double) green);
            cblue = (int) (((double) k / 255D) * (double) blue);
        } else if (i <= -1500 || i > -880) {
            int l = (int) (0.088999999999999996D * (double) (Math.abs(i) - 880)) + 200;
            cred = (int) (((double) l / 255D) * (double) red);
            cgreen = (int) (((double) l / 255D) * (double) green);
            cblue = (int) (((double) l / 255D) * (double) blue);
        } else if (i <= -880 || i > -800) {
            cred = (int) ((double) red * 1.23D);
            cgreen = (int) ((double) green * 1.23D);
            cblue = (int) ((double) blue * 1.23D);
            cred = addcolor(cred, 2);
            cgreen = addcolor(cgreen, 2);
            cblue = addcolor(cblue, 2);
        } else if (i <= -800 || i > -700) {
            cred = (int) ((double) red * 1.23D);
            cgreen = (int) ((double) green * 1.23D);
            cblue = (int) ((double) blue * 1.23D);
            cred = addcolor(cred, 5);
            cgreen = addcolor(cgreen, 5);
            cblue = addcolor(cblue, 5);
        } else if (i <= -700) {
            cred = (int) ((double) red * 1.23D);
            cgreen = (int) ((double) green * 1.23D);
            cblue = (int) ((double) blue * 1.23D);
            cred = addcolor(cred, 10);
            cgreen = addcolor(cgreen, 10);
            cblue = addcolor(cblue, 10);
        } else if (i > -5000) {
            cred = 0;
            cgreen = 0;
            cblue = 0;
            calpha = 0;
        }
    }

    private static int addcolor(int i, int j) {
        j = i + j;
        i = j;
        if (j > 255)
            i = 255;
        j = i;
        if (i < 0)
            j = 0;
        return j;
    }


    private static void getcolorchang() {
        if (a == 0) {
            red = 200;
            blue = 0;
            green += changenum;
            if (green >= 200) {
                green = 200;
                a = 1;
            }
        } else if (a == 1) {
            green = 200;
            red -= changenum;
            if (red <= 0) {
                red = 0;
                a = 2;
            }
        } else if (a == 2) {
            green = 200;
            blue += changenum;
            if (blue >= 200) {
                blue = 200;
                a = 3;
            }
        } else if (a == 3) {
            blue = 200;
            green -= changenum;
            if (green <= 0) {
                green = 0;
                a = 4;
            }
        } else if (a == 4) {
            blue = 200;
            red += changenum;
            if (red >= 200) {
                red = 200;
                a = 5;
            }
        } else if (a == 5) {
            red = 200;
            blue -= changenum;
            if (blue <= 0) {
                blue = 0;
                a = 0;
            }
        }
    }


    public static int getoldRms(byte abyte0[]) {
        int i;
        char c;
        float af[];
        ArrayList<Float> arraylist = new ArrayList<Float>();
        c = '\uDCD8';
        if (abyte0 == null)
            return -9000;
        af = new float[abyte0.length * 4];
        i = 0;

        for (i = 0; i < abyte0.length - 1; i++) {
            af[i * 4] = (w * i) / (abyte0.length - 1);
            af[i * 4 + 1] = h / 2 + ((byte) (abyte0[i] + 128) * (h / 2)) / 128;
            af[i * 4 + 2] = (w * (i + 1)) / (abyte0.length - 1);
            af[i * 4 + 3] = h / 2 + ((byte) (abyte0[i + 1] + 128) * (h / 2)) / 128;
            arraylist.add(af[i * 4 + 3]);
        }
        float f;
        float f2;
        int j;
        int k;
        int l;
        int i1;
        int j1;
        int k1;
        int l1;
        f = 0.0F;
        f2 = 200F;
        l = 0;
        j1 = 0;
        i1 = 0;
        k = 0;
        j = 0;
        i = 0;
        k1 = 0;
        l1 = 1;

        for (l1 = 1; l1 < arraylist.size(); l1++) {
            float f1;
            float f3;
            int i2;
            int j2;
            int k2;
            int l2;
            int i3;
            int j3;
            int k3;
            int l3;
            if (l1 >= (arraylist.size() - 1)) {
                if (k1 > 0)
                    i = -(900 - k1 * 100);
                else if (i > 0)
                    i = -(3300 - i * 80);
                else if (j > 0) {
                    i = -(6000 - j * 80);
                } else {
                    i = c;
                    if (k > 0)
                        i = -(8000 - k * 80);
                }
                Log.e("", (new StringBuilder("rms = ")).append(i).toString());
                if (i != -9000) {
                    music_work_num = music_work_num + 1;
                    if (music_work_num > 60000)
                        music_work_num = 2000;
                    cache = true;
                } else {
                    music_off_num = music_off_num + 1;
                    cache = false;
                }
                return i;
            }
            f3 = f;
            i3 = l;
            j3 = j1;
            k3 = k1;
            i2 = i1;
            j2 = k;
            k2 = j;
            l2 = i;
            if (arraylist.get(l1) > arraylist.get(l1 + 1)) {
                f3 = f;
                i3 = l;
                j3 = j1;
                k3 = k1;
                i2 = i1;
                j2 = k;
                k2 = j;
                l2 = i;
                if (arraylist.get(l1) > arraylist.get(l1 - 1)) {
                    l3 = l + 1;
                    f1 = f;
                    if (arraylist.get(l1) > f)
                        f1 = arraylist.get(l1);
                    l = j1;
                    if (arraylist.get(l1) < (float) HEIGHT)
                        l = j1 + 1;
                    j1 = i1;
                    if (arraylist.get(l1) > (float) HEIGHT) {
                        j1 = i1;
                        if (arraylist.get(l1) <= (float) (HEIGHT + 20))
                            j1 = i1 + 1;
                    }
                    i1 = k;
                    if (arraylist.get(l1) > (float) (HEIGHT + 20)) {
                        i1 = k;
                        if (arraylist.get(l1) <= (float) (HEIGHT + 40))
                            i1 = k + 1;
                    }
                    k = j;
                    if (arraylist.get(l1) > (float) (HEIGHT + 40)) {
                        k = j;
                        if (arraylist.get(l1) <= (float) (HEIGHT + 60))
                            k = j + 1;
                    }
                    j = i;
                    if (arraylist.get(l1) > (float) (HEIGHT + 60)) {
                        j = i;
                        if (arraylist.get(l1) <= (float) (HEIGHT + 80))
                            j = i + 1;
                    }
                    f3 = f1;
                    i3 = l3;
                    j3 = l;
                    k3 = k1;
                    i2 = j1;
                    j2 = i1;
                    k2 = k;
                    l2 = j;
                    if (arraylist.get(l1) > (float) (HEIGHT + 80)) {
                        k3 = k1 + 1;
                        l2 = j;
                        k2 = k;
                        j2 = i1;
                        i2 = j1;
                        j3 = l;
                        i3 = l3;
                        f3 = f1;
                    }
                }
            }
            f1 = f2;
            if (arraylist.get(l1) < arraylist.get(l1 + 1)) {
                f1 = f2;
                if (arraylist.get(l1) < arraylist.get(l1 - 1)) {
                    f1 = f2;
                    if (arraylist.get(l1) < f2)
                        f1 = arraylist.get(l1);
                }
            }
            f = f3;
            f2 = f1;
            l = i3;
            j1 = j3;
            k1 = k3;
            i1 = i2;
            k = j2;
            j = k2;
            i = l2;
        }
        return i;
    }
}
